<?php
/*
$Log: class_Profiler.php,v $
Revision 1.1  2004/09/15 09:25:09  unrz109
import

Revision 1.1  2004/09/15 07:39:54  unrz109
*** empty log message ***

*/
/**
* class to measure the execution time of a PHP-script
* original version by Allan Kent, allan_at_itsolutions_dot_co_dot_za 
*
* @example class_Profiler_example.php Example how to use this class
* @package Profiler
* @author Volker Buzek
* @copyright Regionales Rechenzentrum Erlangen (RRZE) 
* @version $Revision: 1.1 $
*/
class Profiler {

/**
* array to store the information that we collect
* @var array
*/
var $points = array();
    
        /**
        * starts the timer
        *
        * @return void
        * @author Volker Buzek
        * @access public
        * @since 1.0
        * @see addmarker
        */
        function start() {
                $this->addmarker('Start');
        }
        
        /**
        * stops the timer
        *
        * @return void
        * @author Volker Buzek
        * @access public
        * @since 1.0
        * @see addmarker
        */
        function stop() {
                $this->addmarker('Stop');
        }
        
        /**
        * add a marker during execution of the script that is to measure
        *
        * @param string $name an arbitrary name
        * @return void
        * @author Volker Buzek
        * @access public
        * @since 1.0
        */
        function addmarker($name) {
                // call the jointime() function and pass it the output of the microtime() function
                //  as an argument
                $markertime = $this->jointime(microtime());
                // $ae (stands for Array Elements) will contain the number of elements 
                // currently in the $points array
                $ae = count($this->points);
                // store the timestamp and the descriptive name in the array
                $this->points[$ae][0] = $markertime;
                $this->points[$ae][1] = $name;
        }
        
        /**
        * manipulates the string from a call to microtime()
        *
        * @param mixed $mtime the microtime() value
        * @return string 
        * @author Volker Buzek
        * @access private
        * @since 1.0
        */
        function jointime($mtime) {
                // split up the output string from microtime() that has been passed
                // to the function
                $timeparts = explode(' ',$mtime);
                // concatenate the two bits together, dropping the leading 0 from the
                // fractional part
                $finaltime = $timeparts[1].substr($timeparts[0],1);
                // return the concatenated string
                return $finaltime;
        }
        
        /**
        * echos the calculated total in seconds between the start of the script and the end of the script
        *
        * @return void
        * @author Volker Buzek
        * @access public
        * @since 1.0
        */ 
        function showtime() {
                echo bcsub($this->points[count($this->points)-1][0],$this->points[0][0],6);
        }
        
        /**
        * display the collected execution time information in an HTML-table
        *
        * @return void
        * @author Volker Buzek
        * @access public
        * @since 1.0
        */ 
        function debug() {
                echo 'Debug Informationen:';
                echo "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n";
                // the format of our table will be 3 columns:
                // Marker name, Timestamp, difference
                echo "<tr><td><b>Messpunkt</b></td><td><b>Zeit (Timestamp)</b></td><td><b>Unterschied</b></td></tr>\n";
                // the first row will have no difference since it is the first timestamp
                echo "<tr>\n";
                echo '<td>'.$this->points[0][1].'</td>';
                echo '<td>'.$this->points[0][0].'</td>';
                echo "<td>-</td>\n";
                echo "</tr>\n";
                // our loop through the $points array must start at 1 rather than 0 because we have
                // already written out the first row
                $j = count($this->points);
                for ($i = 1; $i < $j;$i++) {
                        echo "<tr>\n";
                        echo '<td>'.$this->points[$i][1].'</td>';
                        echo '<td>'.$this->points[$i][0].'</td>';
                        echo '<td>';
                        // write out the difference between this row and the previous row
                        echo bcsub($this->points[$i][0],$this->points[$i-1][0],6);
                        echo " Sek.";
                        echo '</td>';
                        echo "</tr>\n";
                }
                echo '</table>';
        }
}
?>